package com.be.beadmin.design.controller;


import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.design.dto.output.DrawingReviewEx;
import com.be.beadmin.design.dto.output.DrawingReviewExcelEx;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;

import com.be.beadmin.design.service.DrawingReviewService;

import com.be.beadmin.design.dto.input.DrawingReviewDto;

import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;

/**
* @author zmy
* @date 2023-09-05
**/

@Api("设计管理-图纸审批相关")
@RestController
public class DrawingReviewController {

    @Autowired
    private  DrawingReviewService drawingReviewService;


    @PostMapping(value = "/design/getDrawingReviewByPage")
    public ResultModel getDrawingReviewByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(drawingReviewService.getDrawingReviewByPage(pageEntity)) ;
    }

    @PostMapping("/design/addDrawingReview")
    public ResultModel addDrawingReview(@RequestBody DrawingReviewDto drawingReviewDto){
        drawingReviewService.addDrawingReview(drawingReviewDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @ApiOperation("支持多图纸提交审批")
    @PostMapping("/design/addMutiDrawingReview")
    //  ProductCode 和 DrawingId 要传
    public ResultModel addMutiDrawingReview(@RequestBody List<DrawingReviewDto> drawingReviewDto) throws ParseException {
        drawingReviewService.addMutiDrawingReview(drawingReviewDto);
        return ResultModel.success("提交成功");
    }

    @PostMapping("/design/delDrawingReview")
    public ResultModel delDrawingReview(@RequestBody DrawingReviewDto drawingReviewDto){
        drawingReviewService.delDrawingReview(drawingReviewDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/design/updateDrawingReview")
    public ResultModel updateDrawingReview(@RequestBody DrawingReviewDto drawingReviewDto){
        drawingReviewService.updateDrawingReview(drawingReviewDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/design/exportDrawingReview")
    public void exportDrawingReview(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<DrawingReviewEx> drawingReviewPage = drawingReviewService.getDrawingReviewByPage(pageEntity);
        List<DrawingReviewEx> drawingReviewExes = drawingReviewPage.getContent();
        List<DrawingReviewExcelEx> drawingReviewExcelExes=drawingReviewExes.stream().map(item->{
            DrawingReviewExcelEx excel=new DrawingReviewExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("DrawingReview接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), DrawingReviewExcelEx.class).sheet("sheet1").doWrite(drawingReviewExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), DrawingReviewExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(drawingReviewExcelExes);
    }
    @ApiOperation("确认审核,通过=2；驳回=3")
    @PostMapping("/design/confirmDrawingReview")
    public ResultModel confirmDrawingReview(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            @ApiParam(value = "审核状态", required = true) @RequestParam int reviewState,
            @ApiParam(value = "审核备注") @RequestParam(required = false) String reviewMemo,
            @ApiParam(value = "提交版本") @RequestParam(required = false) String version) throws Exception {

        try {
            Float.valueOf(version);
        } catch (NumberFormatException e) {
            throw new BeadminException("请上传正确的版本格式");
        }
        drawingReviewService.confirmDrawingReview(id,reviewState,reviewMemo,version);
        return ResultModel.success("审核完成！");
    }
}